package com.ykq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykq.core.entity.User;
import com.ykq.core.vo.R;
import com.ykq.system.mapper.UserMapper;
import com.ykq.system.service.UserService;
import com.ykq.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: 闫克起
 * @Description:
 * @Date: Create in 11:51 2024/8/12
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public R<User> selectByName(String username) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",username);
        wrapper.eq("del_flag",0);
        wrapper.eq("status",0);
        User user = userMapper.selectOne(wrapper);
        if(user==null){
            return R.error("账号或密码错误");
        }
        return R.ok(user);
    }

    @Override
    public R<IPage<User>> selectByPage(UserVo userVo) {
        //创建一个分页对象
        IPage<User> page=new Page<>(userVo.getPageNum(), userVo.getPageSize());
        // 创建一个查询对象
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(userVo.getDeptId()!=null){
            wrapper.eq("dept_id",userVo.getDeptId());
        }
        if(StringUtils.hasText(userVo.getUsername())){
            wrapper.like("user_name",userVo.getUsername());
        }
        if(StringUtils.hasText(userVo.getPhone())){
            wrapper.like("phone",userVo.getPhone());
        }
        if(userVo.getStatus()!=null){
            wrapper.eq("status",userVo.getStatus());
        }
        if(userVo.getDateRange()!=null&&userVo.getDateRange().size()==2){
            wrapper.between("create_time",userVo.getDateRange().get(0),userVo.getDateRange().get(1));
        }
        wrapper.eq("del_flag",0);

        IPage<User> userIPage = userMapper.selectPage(page, wrapper);

        return R.ok(userIPage);
    }

    @Override
    public R batchDelete(Long[] userIds) {
        List<User> collect = Arrays.stream(userIds).map(item -> {
            User user = new User();
            user.setUserId(item);
            user.setDelFlag("1");
            return user;
        }).collect(Collectors.toList());

        userMapper.updateById(collect);
        return R.ok();
    }

    @Override
    public R<List<User>> selelctSchedulingUser(Map<String, Object> params) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("scheduling_flag",0);
        if(Objects.nonNull(params.get("userId"))){
            wrapper.eq("user_id",params.get("userId"));
        }
        if(Objects.nonNull(params.get("deptId"))){
            wrapper.eq("dept_id",params.get("deptId"));
        }
        List<User> users = userMapper.selectList(wrapper);
        return R.ok(users);
    }

    @Override
    public R<List<User>> selectAll() {
        List<User> users = userMapper.selectList(null);
        return R.ok(users);
    }
}
